home *** CD-ROM | disk | FTP | other *** search
/ PCGUIA 127 / PC Guia 127.iso / Software / Produtividade / OpenOffice.org 2.0.1 / openofficeorg4.cab / test_class.py < prev    next >
Text File  |  2005-11-19  |  6KB  |  318 lines

  1. "Test the functionality of Python classes implementing operators."
  2.  
  3. from test.test_support import TestFailed
  4.  
  5. testmeths = [
  6.  
  7. # Binary operations
  8.     "add",
  9.     "radd",
  10.     "sub",
  11.     "rsub",
  12.     "mul",
  13.     "rmul",
  14.     "div",
  15.     "rdiv",
  16.     "mod",
  17.     "rmod",
  18.     "divmod",
  19.     "rdivmod",
  20.     "pow",
  21.     "rpow",
  22.     "rshift",
  23.     "rrshift",
  24.     "lshift",
  25.     "rlshift",
  26.     "and",
  27.     "rand",
  28.     "or",
  29.     "ror",
  30.     "xor",
  31.     "rxor",
  32.  
  33. # List/dict operations
  34.     "contains",
  35.     "getitem",
  36.     "getslice",
  37.     "setitem",
  38.     "setslice",
  39.     "delitem",
  40.     "delslice",
  41.  
  42. # Unary operations
  43.     "neg",
  44.     "pos",
  45.     "abs",
  46.     "int",
  47.     "long",
  48.     "float",
  49.     "oct",
  50.     "hex",
  51.  
  52. # generic operations
  53.     "init",
  54.     ]
  55.  
  56. # These need to return something other than None
  57. #    "coerce",
  58. #    "hash",
  59. #    "str",
  60. #    "repr",
  61.  
  62. # These are separate because they can influence the test of other methods.
  63. #    "getattr",
  64. #    "setattr",
  65. #    "delattr",
  66.  
  67. class AllTests:
  68.     def __coerce__(self, *args):
  69.         print "__coerce__:", args
  70.         return (self,) + args
  71.  
  72.     def __hash__(self, *args):
  73.         print "__hash__:", args
  74.         return hash(id(self))
  75.  
  76.     def __str__(self, *args):
  77.         print "__str__:", args
  78.         return "AllTests"
  79.  
  80.     def __repr__(self, *args):
  81.         print "__repr__:", args
  82.         return "AllTests"
  83.  
  84.     def __cmp__(self, *args):
  85.         print "__cmp__:", args
  86.         return 0
  87.  
  88.     def __del__(self, *args):
  89.         print "__del__:", args
  90.  
  91. # Synthesize AllTests methods from the names in testmeths.
  92.  
  93. method_template = """\
  94. def __%(method)s__(self, *args):
  95.     print "__%(method)s__:", args
  96. """
  97.  
  98. for method in testmeths:
  99.     exec method_template % locals() in AllTests.__dict__
  100.  
  101. del method, method_template
  102.  
  103. # this also tests __init__ of course.
  104. testme = AllTests()
  105.  
  106. # Binary operations
  107.  
  108. testme + 1
  109. 1 + testme
  110.  
  111. testme - 1
  112. 1 - testme
  113.  
  114. testme * 1
  115. 1 * testme
  116.  
  117. if 1/2 == 0:
  118.     testme / 1
  119.     1 / testme
  120. else:
  121.     # True division is in effect, so "/" doesn't map to __div__ etc; but
  122.     # the canned expected-output file requires that __div__ etc get called.
  123.     testme.__coerce__(1)
  124.     testme.__div__(1)
  125.     testme.__coerce__(1)
  126.     testme.__rdiv__(1)
  127.  
  128. testme % 1
  129. 1 % testme
  130.  
  131. divmod(testme,1)
  132. divmod(1, testme)
  133.  
  134. testme ** 1
  135. 1 ** testme
  136.  
  137. testme >> 1
  138. 1 >> testme
  139.  
  140. testme << 1
  141. 1 << testme
  142.  
  143. testme & 1
  144. 1 & testme
  145.  
  146. testme | 1
  147. 1 | testme
  148.  
  149. testme ^ 1
  150. 1 ^ testme
  151.  
  152.  
  153. # List/dict operations
  154.  
  155. 1 in testme
  156.  
  157. testme[1]
  158. testme[1] = 1
  159. del testme[1]
  160.  
  161. testme[:42]
  162. testme[:42] = "The Answer"
  163. del testme[:42]
  164.  
  165. testme[2:1024:10]
  166. testme[2:1024:10] = "A lot"
  167. del testme[2:1024:10]
  168.  
  169. testme[:42, ..., :24:, 24, 100]
  170. testme[:42, ..., :24:, 24, 100] = "Strange"
  171. del testme[:42, ..., :24:, 24, 100]
  172.  
  173.  
  174. # Now remove the slice hooks to see if converting normal slices to slice
  175. # object works.
  176.  
  177. del AllTests.__getslice__
  178. del AllTests.__setslice__
  179. del AllTests.__delslice__
  180.  
  181. import sys
  182. if sys.platform[:4] != 'java':
  183.     testme[:42]
  184.     testme[:42] = "The Answer"
  185.     del testme[:42]
  186. else:
  187.     # This works under Jython, but the actual slice values are
  188.     # different.
  189.     print "__getitem__: (slice(0, 42, None),)"
  190.     print "__setitem__: (slice(0, 42, None), 'The Answer')"
  191.     print "__delitem__: (slice(0, 42, None),)"
  192.  
  193. # Unary operations
  194.  
  195. -testme
  196. +testme
  197. abs(testme)
  198. if sys.platform[:4] != 'java':
  199.     int(testme)
  200.     long(testme)
  201.     float(testme)
  202.     oct(testme)
  203.     hex(testme)
  204. else:
  205.     # Jython enforced that these methods return
  206.     # a value of the expected type.
  207.     print "__int__: ()"
  208.     print "__long__: ()"
  209.     print "__float__: ()"
  210.     print "__oct__: ()"
  211.     print "__hex__: ()"
  212.  
  213.  
  214. # And the rest...
  215.  
  216. hash(testme)
  217. repr(testme)
  218. str(testme)
  219.  
  220. testme == 1
  221. testme < 1
  222. testme > 1
  223. testme <> 1
  224. testme != 1
  225. 1 == testme
  226. 1 < testme
  227. 1 > testme
  228. 1 <> testme
  229. 1 != testme
  230.  
  231. # This test has to be last (duh.)
  232.  
  233. del testme
  234. if sys.platform[:4] == 'java':
  235.     import java
  236.     java.lang.System.gc()
  237.  
  238. # Interfering tests
  239.  
  240. class ExtraTests:
  241.     def __getattr__(self, *args):
  242.         print "__getattr__:", args
  243.         return "SomeVal"
  244.  
  245.     def __setattr__(self, *args):
  246.         print "__setattr__:", args
  247.  
  248.     def __delattr__(self, *args):
  249.         print "__delattr__:", args
  250.  
  251. testme = ExtraTests()
  252. testme.spam
  253. testme.eggs = "spam, spam, spam and ham"
  254. del testme.cardinal
  255.  
  256.  
  257. # Test correct errors from hash() on objects with comparisons but no __hash__
  258.  
  259. class C0:
  260.     pass
  261.  
  262. hash(C0()) # This should work; the next two should raise TypeError
  263.  
  264. class C1:
  265.     def __cmp__(self, other): return 0
  266.  
  267. try: hash(C1())
  268. except TypeError: pass
  269. else: raise TestFailed, "hash(C1()) should raise an exception"
  270.  
  271. class C2:
  272.     def __eq__(self, other): return 1
  273.  
  274. try: hash(C2())
  275. except TypeError: pass
  276. else: raise TestFailed, "hash(C2()) should raise an exception"
  277.  
  278.  
  279. # Test for SF bug 532646
  280.  
  281. class A:
  282.     pass
  283. A.__call__ = A()
  284. a = A()
  285. try:
  286.     a() # This should not segfault
  287. except RuntimeError:
  288.     pass
  289. else:
  290.     raise TestFailed, "how could this not have overflowed the stack?"
  291.  
  292.  
  293. # Tests for exceptions raised in instance_getattr2().
  294.  
  295. def booh(self):
  296.     raise AttributeError, "booh"
  297.  
  298. class A:
  299.     a = property(booh)
  300. try:
  301.     A().a # Raised AttributeError: A instance has no attribute 'a'
  302. except AttributeError, x:
  303.     if str(x) is not "booh":
  304.         print "attribute error for A().a got masked:", str(x)
  305.  
  306. class E:
  307.     __eq__ = property(booh)
  308. E() == E() # In debug mode, caused a C-level assert() to fail
  309.  
  310. class I:
  311.     __init__ = property(booh)
  312. try:
  313.     I() # In debug mode, printed XXX undetected error and raises AttributeError
  314. except AttributeError, x:
  315.     pass
  316. else:
  317.     print "attribute error for I.__init__ got masked"
  318.